<?php
namespace app\controller;

use app\BaseController;
use think\facade\Request;
use think\facade\Db;
use think\Exception;


class StoneInfo extends BaseController
{
    //获取宝石大类信息名称
    public function Get_StoneInfo()
    {
        $data=[
            [
                'name'=>'宝石分类',
                'table'=>'SET_StoneCategory'
            ],
            [
                'name'=>'宝石净度',
                'table'=>'SET_StoneClarity'
            ],
            [
                'name'=>'宝石切工',
                'table'=>'SET_StoneCut'
            ],
            [
                'name'=>'宝石颜色',
                'table'=>'SET_StoneColor'
            ],
            [
                'name'=>'宝石形状',
                'table'=>'SET_StoneShape'
            ],
            [

                'name'=>'宝石镶嵌',
                'table'=>'SET_StoneInlay'

            ],
            [

                'name'=>'宝石名称',
                'table'=>'SET_StoneName'

            ],


        ];




        return json([
            'code'=>200,
            'msg'=>'获取成功',
            'data'=>$data
        ]);
    }


    //获取点击宝石表格具体的信息
    public function Get_StoneInfoTable()
    {
        $current_page = Request::param('current_page');
        $page_size = Request::param('page_size');
        $table = Request::param('table');

        $header=[];
        switch ($table){
            case 'SET_StoneCategory':
                $header=[
                    [
                        'prop'=>'StoneCategory_name',
                        'lable'=>'分类名称',

                    ],
                    [
                        'prop'=>'StoneCategory_category',
                        'lable'=>'大类',

                    ],
                    [
                        'prop'=>'StoneCategory_weightunit',
                        'lable'=>'重量单位',

                    ],
                    [
                        'prop'=>'StoneCategory_remark',
                        'lable'=>'备注',

                    ],
                    [
                        'switch'=>true,
                        'prop'=>'StoneCategory_switch',
                        'lable'=>'开关',

                    ],

                      ];
                break;
            case 'SET_StoneClarity':
                $header=[
                    [
                        'prop'=>'StoneClarity_name',
                        'lable'=>'净度名称',

                    ],
                    [
                        'prop'=>'StoneClarity_remark',
                        'lable'=>'备注',

                    ],
                    [
                        'switch'=>true,
                        'prop'=>'StoneClarity_switch',
                        'lable'=>'开关',

                    ],

                ];
                break;
            case 'SET_StoneColor':
                $header=[
                    [
                        'prop'=>'StoneColor_name',
                        'lable'=>'颜色名称',

                    ],
                    [
                        'prop'=>'StoneColor_remark',
                        'lable'=>'备注',

                    ],
                    [
                        'switch'=>true,
                        'prop'=>'StoneColor_switch',
                        'lable'=>'开关',

                    ],

                ];
                break;
            case 'SET_StoneCut':
                $header=[
                    [
                        'prop'=>'StoneCut_name',
                        'lable'=>'切工名称',

                    ],
                    [
                        'prop'=>'StoneCut_remark',
                        'lable'=>'备注',

                    ],
                    [
                        'switch'=>true,
                        'prop'=>'StoneCut_switch',
                        'lable'=>'开关',

                    ],

                ];
                break;
            case 'SET_StoneInlay':
                $header=[
                    [
                        'prop'=>'StoneInlay_name',
                        'lable'=>'镶法名称',

                    ],
                    [
                        'prop'=>'StoneInlay_remark',
                        'lable'=>'备注',

                    ],
                    [
                        'switch'=>true,
                        'prop'=>'StoneInlay_switch',
                        'lable'=>'开关',

                    ],

                ];
                break;
            case 'SET_StoneName':
                $header=[
                    [
                        'prop'=>'StoneName_name',
                        'lable'=>'宝石名称',

                    ],
                    [
                        'prop'=>'StoneName_remark',
                        'lable'=>'备注',

                    ],
                    [
                        'switch'=>true,
                        'prop'=>'StoneName_switch',
                        'lable'=>'开关',

                    ],
                    [
                        'prop'=>'StoneCategory_name',
                        'lable'=>'大类名称',

                    ],

                ];
                break;
            case 'SET_StoneShape':
                $header=[
                    [
                        'prop'=>'StoneShape_name',
                        'lable'=>'形状名称',

                    ],
                    [
                        'prop'=>'StoneShape_remark',
                        'lable'=>'备注',

                    ],
                    [
                        'switch'=>true,
                        'prop'=>'StoneShape_switch',
                        'lable'=>'开关',

                    ],

                ];
                break;
        }

        $userId = $this->request->auth['data']->user->user_id;
        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');


        try{
            $data = Db::table($table)->where(substr($table, strlen('SET_')).'_company_id',$companyId)->page($current_page,$page_size)->select();
            $count = Db::table($table)->where(substr($table, strlen('SET_')).'_company_id',$companyId)->count();

            $StoneCategory=[];

            if($table=='SET_StoneName'){

                //获取SET_StoneCategory表全部数据
                $StoneCategory=Db::table('SET_StoneCategory')->where('StoneCategory_company_id',$companyId)->select();

                //根据SET_StoneName表的StoneName_StoneCategory_id获取链表查询SET_StoneCategory表
                $data = Db::table($table)->alias('a')->join('SET_StoneCategory b','a.StoneName_StoneCategory_id=b.StoneCategory_id')->where(substr($table, strlen('SET_')).'_company_id',$companyId)->page($current_page,$page_size)->select();

            }




            return json([
                'code'=>200,
                'msg'=>'获取成功',
                'header'=>$header,
                'data'=>[
                    'data'=>$data,
                    'total'=>$count,
                    'StoneCategory'=>$StoneCategory
                ]
            ]);
        }catch (Exception $e){
            return json([
                'code'=>500,
                'msg'=>'获取失败',
                'data'=>$e->getMessage()
                ]);
            }


    }

    //新增宝石信息，要考虑新增失败回滚问题,要开启事务
    public function Insert_StoneInfo(){

        $table = $this->request->param('table');
        $data = $this->request->param('data');
        $userId = $this->request->auth['data']->user->user_id;

        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        $data[substr($table, strlen('SET_')).'_company_id']=$companyId;
        $data[substr($table, strlen('SET_')).'_switch']=1;

        //判断商品名称是否重复
        if(Db::table($table)->where(substr($table, strlen('SET_')).'_name',$data[substr($table, strlen('SET_')).'_name'])->where(substr($table, strlen('SET_')).'_company_id',$companyId)->find())
        {

            return json([
                'code'=>501,
                'msg'=>'名称重复'
            ]);

        }

        try{
            Db::startTrans();
            $res = Db::table($table)->insert($data);
            Db::commit();
            if($res){
                return json([
                    'code'=>200,
                    'msg'=>'新增成功',
                    'data'=>$res
                ]);
            }else{
                return json([
                    'code'=>500,
                    'msg'=>'新增失败',
                    'data'=>$res
                ]);
            }
        }catch (Exception $e){
            // 回滚事务
            Db::rollback();
            return json([
                'code'=>500,
                'msg'=>'新增失败'.$e->getMessage(),
                'data'=>$e->getMessage()
            ]);
        }


    }

    //修改宝石信息，要考虑新增失败回滚问题,要开启事务
    public function Update_StoneInfo(){
        $id = $this->request->param('id');
        $data = $this->request->param('data');
        $table = $this->request->param('table');

        $userId = $this->request->auth['data']->user->user_id;

        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');


        if (isset($data['switch']))
        {
            $info = Db::table($table)
                ->where(substr($table, strlen('SET_')) . '_id', '<>', $id['value'])
                ->where(substr($table, strlen('SET_')) . '_name', $data[substr($table, strlen('SET_')) . '_name'])
                ->where(substr($table, strlen('SET_')) . '_company_id', $companyId)
                ->find();

            if ($info) {
                return json([
                    'code' => 400,
                    'msg' => '名称已存在',
                ]);
            }
        }



        try {
            Db::startTrans();
            $res = Db::table($table)->where(substr($table, strlen('SET_')).'_id',$id['value'])->update($data);
            if($res){
                Db::commit();
                return json([
                    'code'=>200,
                    'msg'=>'修改成功',
                    'data'=>$res
                ]);
            }else{
                Db::rollback();
                return json([
                    'code'=>500,
                    'msg'=>'修改失败',
                    'data'=>$res
                ]);

            }
        } catch (Exception $e){
            // 回滚事务
            Db::rollback();
            return json([
                'code'=>500,
                'msg'=>'修改失败'.$e->getMessage(),
                'data'=>$e->getMessage()
            ]);
            }
    }

    //删除宝石信息，要考虑新增失败回滚问题,要开启事务
    public function Delete_StoneInfo(){
        $id = $this->request->param('id');
        $table = $this->request->param('table');

        try {
            Db::startTrans();
            $res = Db::table($table)->where(substr($table, strlen('SET_')).'_id',$id)->delete($id);
            if($res){
                Db::commit();
                return json([
                    'code'=>200,
                    'msg'=>"删除成功",
                    'data'=>$res
                ]);
            }else{
                Db::rollback();
                return json([
                    'code'=>500,
                    'msg'=>"删除失败",
                    'data'=>$res
                ]);

                }
            }
            catch (Exception $e){
                // 回滚事务
                Db::rollback();
                if($e->getCode()==10501)
                {
                    return json(['code' => 500, 'msg' => '该宝石信息下已存在单据，无法删除']);
                }
                return json([
                    'code'=>500,
                    'msg'=>'删除失败'.$e->getMessage(),
                    'data'=>$e->getMessage()
                ]);
            }

    }
}